package com.hsource.hsourcegateway.exception;

import org.springframework.web.server.ResponseStatusException;

import org.springframework.cloud.gateway.support.NotFoundException;

import java.util.HashMap;
import java.util.Map;


/**
 * @Author itoug
 * @Crreate 2020-10-20 18:16
 */
    public abstract class AbstractExceptionHandler {

        private static final String DEFAULT_ERROR_CODE = "1";

        protected String formatMessage(Throwable ex) {
            String errorMessage = null;
            if (ex instanceof NotFoundException) {
                String reason = ((NotFoundException) ex).getReason();
                errorMessage = reason;
            } else if (ex instanceof ResponseStatusException) {
                ResponseStatusException responseStatusException = (ResponseStatusException) ex;
                errorMessage = responseStatusException.getMessage();
            } else {
                errorMessage = ex.getMessage();
            }
            return errorMessage;
        }

        protected Map<String, Object> buildErrorMap(String errorMessage) {
            Map<String, Object> resMap = new HashMap<>();
            resMap.put("success", false);
            resMap.put("code", DEFAULT_ERROR_CODE);
            resMap.put("message", errorMessage);
            resMap.put("data", null);
            resMap.put("auth",null);
            return resMap;
        }

    }

